#include <jni.h>
#include <string>
#include "EasyPlayer.h"

extern "C" {
#include <libavcodec/avcodec.h>
}

// 播放器
EasyPlayer *player = nullptr;
JavaVM *javaVM = nullptr;

int JNI_OnLoad(JavaVM *vm, void *r) {
    javaVM = vm;
    return JNI_VERSION_1_6;
}

// 获取版本号，检测ffmpeg环境是否搭建成功
extern "C"
JNIEXPORT jstring JNICALL
Java_com_fauks_ndk_player_EasyPlayer_getVersionInfo(JNIEnv *env, jobject thiz) {
    return env->NewStringUTF(av_version_info());
}

// 准备播放源
extern "C"
JNIEXPORT void JNICALL
Java_com_fauks_ndk_player_EasyPlayer_nativePrepare(JNIEnv *env, jobject thiz, jstring data_source) {
    const char *dataSource = env->GetStringUTFChars(data_source, nullptr);
    auto *helper = new JavaCallHelper(javaVM, env, thiz);
    // 创建播放器
    player = new EasyPlayer(helper, dataSource);
    player->prepare();
    env->ReleaseStringUTFChars(data_source, dataSource);
}